package cn;

import cn.webrx.entity.Book;
import cn.webrx.mapper.BookMapper;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.sql.DataSource;
import java.util.List;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-09-15 12:03
 */
public class MyDemo {
    protected SqlSession session;
    protected SqlSessionFactory sf;
    @Before
    public void init(){
        DataSource ds = new PooledDataSource("com.mysql.cj.jdbc.Driver","jdbc:mysql:/db?serverTimezone=PRC","root","");
        TransactionFactory tf = new JdbcTransactionFactory();
        Environment ev = new Environment("default", tf, ds);
        Configuration cfg = new Configuration(ev);
        cfg.addMappers("cn.webrx.mapper");
        sf = new SqlSessionFactoryBuilder().build(cfg);
        session = sf.openSession();
    }
    @Test
    public void show(){
        BookMapper bookdao = session.getMapper(BookMapper.class);
        List<Book> list = bookdao.queryAll();
        System.out.println(list.size());
        for(Book book : list){
            System.out.println(book.getName());
        }

        System.out.println("-".repeat(50));
        System.out.println(bookdao.show("%java%"));
        System.out.println("-".repeat(50));


        System.out.println(session.selectList("abc","%java%"));
    }
    @After
    public void close(){
        session.commit();
        session.close();
    }
}
